//
//  CourseHeaderView.m
//  PhpwsdClient
//
//  Created by 黄思清 on 2017/3/8.
//  Copyright © 2017年 com.phpwsd. All rights reserved.
//

#import "CourseHeaderView.h"

@interface CourseHeaderView ()
@property (assign, nonatomic) CGFloat headerViewHeight;     // headerView 高度
@property (assign, nonatomic) CGFloat screenWidth;

@end

@implementation CourseHeaderView

- (UIImageView *)imageV {
    if (_imageV == nil) {
        UIImage *avatar = [[UIImage alloc] init];
        _imageV = [[UIImageView alloc] initWithImage:avatar];
        _imageV.userInteractionEnabled = true;
    }
    return _imageV;
}

-(UILabel *)courseTeacher{
    if(_courseTeacher==nil){
        _courseTeacher = [[UILabel alloc]init];
    }
    return _courseTeacher;
}

-(UILabel *)courseLabel{
    if(_courseLabel==nil){
        _courseLabel = [[UILabel alloc]init];
        _courseLabel.numberOfLines = 2;
    }
    return _courseLabel;
}

- (instancetype)init {
    if (self = [super init]) {
        self.headbgView = [[UIView alloc]init];
        
        UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"avatar"]];
        self.headbgView.backgroundColor = bgColor;

        self.shadebgView = [[UIImageView alloc]init];
        [self.shadebgView setImage:[UIImage imageNamed:@"translucence"]];
        self.shadebgView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.headbgView];

        [self addSubview:self.shadebgView];

        UIView *blackV = [[UIView alloc]init];
        blackV.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
        [self addSubview:blackV];

        [blackV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self);
            make.bottom.equalTo(self);
        }];


        self.backgroundColor = [UIColor clearColor];
        self.headerViewHeight = 200;
        self.screenWidth = [UIScreen mainScreen].bounds.size.width;
        [self addSubview:self.imageV];
        
        [self addSubview:self.courseTeacher];
        [self addSubview:self.courseLabel];
        
        self.bottomView = [[UIView alloc]init];
        self.leftBtn    = [[UIButton alloc]init];
        self.centerBtn  = [[UIButton alloc]init];
        self.rightBtn   = [[UIButton alloc]init];
        
        [self.bottomView addSubview:self.leftBtn];
        [self.bottomView addSubview:self.centerBtn];
        [self.bottomView addSubview:self.rightBtn];
        
        [self addSubview:self.bottomView];
    }
    return self;
}

// 更改 frame 会触发 layoutSubviews
// 触发 layoutSubviews 后，这个 view 里面的控件想怎么变（旋转，位移，缩放），全部这个方法里面就好了
- (void)layoutSubviews {
    [super layoutSubviews];
 
    self.headbgView.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 200);
    self.shadebgView.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 200);
    self.imageV.frame = CGRectMake(10, 80, 60, 60);
    self.courseLabel.textColor = [UIColor whiteColor];
    self.courseTeacher.textColor = [UIColor whiteColor];
    [self.courseTeacher setFont:[UIFont systemFontOfSize:14]];
    
    [self.bottomView setBackgroundColor:[UIColor blackColor]];
    
    [self.leftBtn setTitle:@"基础信息" forState:UIControlStateNormal];
    [self.leftBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.leftBtn setTag:1];
    
    [self.centerBtn setTitle:@"教学大纲" forState:UIControlStateNormal];
    [self.centerBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.centerBtn setTag:2];
    
    [self.rightBtn setTitle:@"学习小结" forState:UIControlStateNormal];
    [self.rightBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.rightBtn setTag:3];
    
    [self.leftBtn setBackgroundColor:UIColors(MainColor)];
    [self.courseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageV.mas_right).offset(10);
        make.centerY.equalTo(self.imageV.mas_centerY);
        make.width.mas_equalTo(150);
//        make.height.mas_equalTo(20);
    }];
    
    [self.courseTeacher mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-5);
        make.centerY.equalTo(self.imageV.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(0);
        make.height.mas_equalTo(44);
        make.right.equalTo(self.mas_right).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];
    
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomView.mas_left);
        make.right.equalTo(self.centerBtn.mas_left).offset(0);
        make.height.mas_equalTo(44);
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.width.equalTo(self.centerBtn.mas_width);
    }];
    
    [self.centerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftBtn.mas_right);
        make.right.equalTo(self.rightBtn.mas_left).offset(0);
        make.height.mas_equalTo(44);
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.width.equalTo(self.rightBtn.mas_width);
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.centerBtn.mas_right);
        make.right.equalTo(self.bottomView.mas_right).offset(0);
        make.height.mas_equalTo(44);
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.width.equalTo(self.centerBtn.mas_width);
    }];
    
    
    [self setNeedsDisplay];     // 重绘
}


@end
